package com.learn.utils;

import com.learn.config.VideoConfig;
import com.tencentcloudapi.asr.v20190614.models.*;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.asr.v20190614.AsrClient;
import com.tencentcloudapi.common.profile.HttpProfile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class TenXunYunUtil {

    @Autowired
    private VideoConfig videoConfig;

    /**
     * 多的一个请求Asc产品的对象
     * * * */
    public  AsrClient getAsrClient(){
        Credential cred = new Credential(videoConfig.getSecretId(), videoConfig.getSecretKey());
        // 实例化一个http选项，可选的，没有特殊需求可以跳过
        HttpProfile httpProfile = new HttpProfile();
        httpProfile.setEndpoint("asr.tencentcloudapi.com");
        // 实例化一个client选项，可选的，没有特殊需求可以跳过
        ClientProfile clientProfile = new ClientProfile();
        clientProfile.setHttpProfile(httpProfile);
        // 实例化要请求产品的client对象,clientProfile是可选的
        AsrClient client = new AsrClient(cred, "",clientProfile);
        return client;
    }

    /**
     * 发起语音转文字请求
     * 返回taskID*
     * * * */
    public  Long getClient(String url){
       try {

           AsrClient client = getAsrClient();
           // 实例化一个请求对象,每个接口都会对应一个request对象
           CreateRecTaskRequest req = new CreateRecTaskRequest();
           req.setEngineModelType("16k_zh");
           req.setChannelNum(1L);
           req.setResTextFormat(0L);
           req.setSourceType(0L);
           req.setUrl(url);
//           设置请求结果回调地址，需要公网可访问
//           req.setCallbackUrl("127.0.0.1:8081/learn/url");
           // 返回的resp是一个CreateRecTaskResponse的实例，与请求对象对应
           CreateRecTaskResponse resp = client.CreateRecTask(req);

           return resp.getData().getTaskId();
       }catch (Exception e){
           System.out.println(e.getMessage());
       }
        return null;
    }

/**
 * 主动取请求语音转文字除了结果
 * * * */
    public  TaskStatus getText(Long taskId){
        try {
            AsrClient client = getAsrClient();

            DescribeTaskStatusRequest request = new DescribeTaskStatusRequest();
            request.setTaskId(taskId);
            DescribeTaskStatusResponse statusResponse = client.DescribeTaskStatus(request);

            TaskStatus data = statusResponse.getData();
            return statusResponse.getData();
        }catch (Exception e){
            System.out.println(e.getMessage());
        }
        return null;
    }
}
